package cn.hk.vaccination.service.impl;

import cn.hk.dept.domain.Dept;
import cn.hk.dept.service.DeptService;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.service.UserInfoService;
import cn.hk.vaccination.domain.SubscribeDetail;
import cn.hk.vaccination.domain.SubscribeInfo;
import cn.hk.vaccination.domain.vo.OrderRecorderVo;
import cn.hk.vaccination.domain.vo.VaccinationVo;
import cn.hk.vaccination.mapper.SubscribeInfoMapper;
import cn.hk.vaccination.service.SubscribeDetailService;
import cn.hk.vaccination.service.SubscribeInfoService;
import cn.hk.vaccination.service.VaccinationService;
import cn.hk.vaccination.service.VaccinesPlanService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 预约信息表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-12
 */
@Service
public class SubscribeInfoServiceImpl extends ServiceImpl<SubscribeInfoMapper, SubscribeInfo> implements SubscribeInfoService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private VaccinationService vaccinationService;

    @Autowired
    private VaccinesPlanService vaccinesPlanService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SubscribeInfoMapper subscribeInfoMapper;

    @Autowired
    private SubscribeDetailService subscribeDetailService;

    @Override
    @Transactional
    public AjaxResult vaccineOder(SubscribeInfo subscribeInfo) {
        List<SubscribeInfo> subscribeInfos = subscribeInfoMapper.selectToDoListByUserId(subscribeInfo.getUserId());
        for (SubscribeInfo info : subscribeInfos) {
            if (info.getSubscribeTime().equals(subscribeInfo.getSubscribeTime())) {
                return AjaxResult.error(501, "一天内只能预约一次疫苗");
            }
        }
        //预约状态
        subscribeInfo.setState(1);
        //创建时间
        subscribeInfo.setCreateTime(DateUtil.date().toString());
        //是否签到
        subscribeInfo.setIsAbsence(0);
        //预约号
        List<String> subscribeCodeList = subscribeInfoMapper.selectSubscribeCodeListByDeptIdAndSubscribeTime(subscribeInfo.getId(), subscribeInfo.getSubscribeTime());
        while (true) {
            String subscribeCode = "B" + RandomUtil.randomInt(0, 999);
            if (!CollUtil.contains(subscribeCodeList, subscribeCode)) {
                subscribeInfo.setSubscribeCode(subscribeCode);
                break;
            }
        }
        subscribeInfo.setSeeDoctorState(0);
        subscribeInfo.setOverNumber(0);
        subscribeInfo.setOverNumberBq(0);
        subscribeInfo.setIsDoctorNum(0);

        boolean r = false;
        r = subscribeInfoMapper.insertOne(subscribeInfo);
        if (r) {
            SubscribeDetail subscribeDetail = subscribeDetailService.getOneById(subscribeInfo.getSubscribeDetailId());
            //上午
            if ("1".equals(subscribeDetail.getApFlag()) && subscribeDetail.getSubValue() > 0) {
                Integer subValue = subscribeDetail.getSubValue() - 1;

                SubscribeDetail subscribeDetail2AmValue = new SubscribeDetail();
                subscribeDetail2AmValue.setId(subscribeDetail.getId());
                subscribeDetail2AmValue.setSubValue(subValue);
                subscribeDetail2AmValue.setSoltTime(subscribeDetail.getSoltTime());

                Boolean result = subscribeDetailService.editById(subscribeDetail2AmValue);
                return result ? AjaxResult.success("预约成功") : AjaxResult.error("预约失败");
                //下午
            } else if ("2".equals(subscribeDetail.getApFlag()) && subscribeDetail.getSubValue() > 0) {
                Integer subValue = subscribeDetail.getSubValue() - 1;

                SubscribeDetail subscribeDetail2PmValue = new SubscribeDetail();
                subscribeDetail2PmValue.setId(subscribeDetail.getId());
                subscribeDetail2PmValue.setSubValue(subValue);
                subscribeDetail2PmValue.setSoltTime(subscribeDetail.getSoltTime());

                Boolean result = subscribeDetailService.editById(subscribeDetail2PmValue);
                return result ? AjaxResult.success("预约成功") : AjaxResult.error("预约失败");
            } else {
                return AjaxResult.error("预约失败");
            }
        }

        return AjaxResult.error("预约失败");
    }

    @Override
    public List<SubscribeInfo> getVaccineOderList(Long userId) {
        //存放返回结果
        List<SubscribeInfo> resultList = CollUtil.newArrayList();

        List<SubscribeInfo> subscribeInfoList = CollUtil.newArrayList();
        try {
            subscribeInfoList = subscribeInfoMapper.selectListByUserId(userId);
            UserInfo userInfo = userInfoService.getOneById(userId);
            if (subscribeInfoList.size() > 0) {
                for (SubscribeInfo subscribeInfo : subscribeInfoList) {
                    List<VaccinationVo> vaccinationVoList = vaccinationService.getVaccinationVoListByDeptIdAndUser(userInfo, subscribeInfo.getCommunityId());
                    for (VaccinationVo vaccinationVo : vaccinationVoList) {
                        if (subscribeInfo.getPlanId().equals(vaccinationVo.getPid())) {
                            subscribeInfo.setPname(vaccinationVo.getPname());
                        }
                    }
                }
            }

            for (SubscribeInfo subscribeInfo : subscribeInfoList) {
                Long daysBetweeen = DateUtil.between(DateUtil.parse(subscribeInfo.getSubscribeTime()), DateUtil.date(), DateUnit.DAY, false);
                if (daysBetweeen <= 0) {
                    resultList.add(subscribeInfo);
                }
            }

        } catch (Exception e) {
            Console.log("发生了异常", e);
        }

        return resultList;
    }

    @Override
    public List<OrderRecorderVo> getVaccineRecordList(Long userId) {
        List<OrderRecorderVo> vaccineRecordList = subscribeInfoMapper.selectOrderRecorderListByUserId(userId);
        return vaccineRecordList;
    }

    @Override
    public List<SubscribeInfo> getVaccineOderToDoList(Long userId) {
        List<SubscribeInfo> resultList = CollUtil.newArrayList();
        List<SubscribeInfo> subscribeInfos = subscribeInfoMapper.selectToDoListByUserId(userId);

        for (SubscribeInfo subscribeInfo : subscribeInfos) {
            Dept dept = deptService.getOneByDeptId(subscribeInfo.getCommunityId());
            subscribeInfo.setCname(dept.getDeptName());
            Long daysBetweeen = DateUtil.between(DateUtil.parse(subscribeInfo.getSubscribeTime()), DateUtil.date(), DateUnit.DAY, false);
            if (daysBetweeen <= 0) {
                resultList.add(subscribeInfo);
            }
        }
        return resultList;
    }

    @Override
    public Boolean signIn(Long id) {
        return subscribeInfoMapper.signIn(id);
    }

    @Override
    public Boolean cancelOder(Long id) {
        return subscribeInfoMapper.cancelOder(id);
    }

    @Override
    public SubscribeInfo getOneById(Long id) {
        return subscribeInfoMapper.selectOneById(id);
    }


}
